﻿using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

[Table("ReadingRecords")]
public class ReadingRecord
{
    // 阅读记录ID
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    // 用户ID
    [Required]
    public int UserId { get; set; }

    // 书籍ID
    [Required]
    public int BookId { get; set; }

    // 阅读位置
    [Required]
    public int ReadPos { get; set; }

    // 更新时间
    [Required]
    public DateTime UpdateTime { get; set; }

    // 创建时间
    [Required]
    public DateTime CreateTime { get; set; }

    // 导航属性，用于与User实体进行关联
    public User User { get; set; }

    // 导航属性，用于与Book实体进行关联
    public Book Book { get; set; }

    // 静态方法用于配置实体
    public static void Configure(EntityTypeBuilder<ReadingRecord> builder)
    {
        builder.HasKey(r => r.Id);
        builder.Property(r => r.Id).ValueGeneratedOnAdd();

        builder.Property(r => r.UserId).IsRequired();
        builder.Property(r => r.BookId).IsRequired();
        builder.Property(r => r.ReadPos).IsRequired();
        builder.Property(r => r.UpdateTime).IsRequired();
        builder.Property(r => r.CreateTime).IsRequired();

        // 定义与User实体的关系
        builder.HasOne(r => r.User)
            .WithMany(u => u.ReadingRecords) // 假设User实体有一个名为ReadingRecords的集合导航属性
            .HasForeignKey(r => r.UserId)
            .OnDelete(DeleteBehavior.Restrict); // 防止级联删除
    }
}